import { createRouter, createWebHistory } from 'vue-router';
import Attendance from '@/components/Attendance.vue';
import WeeklyClass from '@/views/WeeklyClass.vue';
import CourseAttendance from '@/components/CourseAttendance.vue';


const routes = [
  {
    path: '/',
    component: () => import('@/Home.vue'),
    meta: { requiresAuth: true },
    children: [
      {
            path: '',
            redirect: '/weekly-class'
          },
          {
            path: 'attendance',
            component: Attendance,
            meta: { title: '出勤统计' }
          },
          {
            path: 'weekly-class',
            component: WeeklyClass,
            meta: { title: '每日课堂' }
          },
          {
            path: 'course-attendance',
            component: CourseAttendance,
            meta: { title: '课程出勤' }
          },
        ]
  },
  {
    path: '/login',
    component: () => import('@/components/Login.vue')
  },
  {
    path: '/other-link',
    meta: { requiresAuth: true },
    component: () => import('@/views/OtherLink.vue')
  },
  {
    path: '/add-course-schedule/clazzid/:clazzId',  // 添加某班级的课程安排
    name: 'AddCourseSchedule',
    component: () => import('../views/AddCourseSchedule.vue')
  },
  {
    path: '/view-course-schedules/clazzid/:clazzId',  // 查看某班级的课程安排
    name: 'ViewCourseSchedules',
    component: () => import('../views/ViewCourseSchedules.vue')
  },
  {
    path: '/add-student-messages/clazzid/:clazzId',  // 添加某班级的学生信息
    name: 'AddStudentMessages',
    component: () => import('../views/AddStudentMessages.vue')
  },
  {
    path: '/view-students/clazzid/:clazzId',  // 查看某班级的学生列表
    name: 'ViewStudents',
    component: () => import('../views/ViewStudents.vue')
  },

  {
    path: '/view-attendance-list/week/:week/day/:day/clazzid/:clazzId',  // 查看某班级的考勤列表
    name: 'ViewAttendanceList',
    component: () => import('@/views/ViewAttendanceList.vue')
  },
  {
    path: '/view-clazz-list',  // 查看班级列表
    name: 'ViewClazzList',
    component: () => import('@/views/ViewClazzList.vue')
  },
  {
    path: '/edit-attendance/:id',//id是为课程安排id
    component: () => import('@/views/EditAttendance.vue'),
    name: 'EditAttendance'
},
{
    path: '/view-attendance/:id',
    component: () => import('@/views/ViewAttendance.vue'),
    name: 'ViewAttendance'
},
{
  path: '/add-clazz',  // 添加班级信息
  name: 'AddClazz',
  component: () => import('@/views/AddClazz.vue')
}
];

const router = createRouter({
  history: createWebHistory(),
  routes
});


router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token');
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

export default router;